package org.gnyxy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.gnyxy.constant.DeleteConstant;
import org.gnyxy.constant.MessageConstant;
import org.gnyxy.constant.StatusConstant;
import org.gnyxy.dto.PageQueryDTO;
import org.gnyxy.entity.Type;
import org.gnyxy.exception.BaseException;
import org.gnyxy.mapper.TypeMapper;
import org.gnyxy.result.PageResult;
import org.gnyxy.service.TypeService;
import org.gnyxy.vo.TypeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    /**
     * 添加分类
     * @param type
     */
    @Override
    public void add(Type type) {
        type.setDeleted(DeleteConstant.IS_NOT_DELETED);
        // 如果不传入状态 默认为禁用
        if (type.getStatus() == null){
            type.setStatus(StatusConstant.DISABLE);
        }
        typeMapper.insert(type);
    }

    /**
     * 分页查询
     * @param pageQueryDTO
     * @return
     */
    @Override
    public PageResult page(PageQueryDTO pageQueryDTO) {
        // 分页
        PageHelper.startPage(pageQueryDTO.getPage(), pageQueryDTO.getPageSize());
        Page<Type> result = typeMapper.pageQuery(pageQueryDTO);
        List<Type> types = result.getResult();
        // 转成VO
        List<TypeVO> typeVOS = types.stream().map(type -> {
            TypeVO typeVO = new TypeVO();
            BeanUtils.copyProperties(type,typeVO);
            return typeVO;
        }).collect(Collectors.toList());
        return new PageResult(result.getTotal(), typeVOS);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public TypeVO getById(Long id) {
        // 查询
        Type type = typeMapper.getById(id);
        // 创建vo并拷贝
        TypeVO typeVO = new TypeVO();
        BeanUtils.copyProperties(type,typeVO);
        return typeVO;
    }

    @Override
    public void update(Type type) {
        // 判断不能为 null
        if (type == null){
            throw new BaseException(MessageConstant.TYPE_IS_NULL);
        }
        typeMapper.update(type);
    }
    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 校验id不为空
        if (id == null){
            throw new BaseException(MessageConstant.ID_IS_NULL);
        }
        // 查询 type
        Type type = typeMapper.getById(id);
        type.setDeleted(DeleteConstant.IS_DELETED);
        typeMapper.update(type);
    }

    /**
     * 设置状态
     * @param status
     * @param id
     */
    @Override
    public void setStatus(Integer status, Long id) {
        Type type = typeMapper.getById(id);
        type.setStatus(status);
        typeMapper.update(type);
    }

    /**
     * 查看未被禁用的分类
     * @return
     */
    @Override
    public List<TypeVO> getTrueStatus(String name) {
        List<Type> types = typeMapper.getTrueStatus(name);
        return types.stream().map(type -> {
            TypeVO typeVO = new TypeVO();
            BeanUtils.copyProperties(type,typeVO);
            return typeVO;
        }).collect(Collectors.toList());
    }

    /**
     * 查看分类
     * @return
     */
    @Override
    public List<TypeVO> getType() {
        List<Type> types = typeMapper.getType();
        //转换成VO
        return types.stream().map(type -> {
            TypeVO typeVO = new TypeVO();
            BeanUtils.copyProperties(type,typeVO);
            return typeVO;
        }).collect(Collectors.toList());
    }


}
